#!/usr/bin/perl

use strict;

# This script is to automatically go through any .RFI RPFITs files
# that are in the current directory and make RFI plots from them.

# Step 1
# Find the RPFITs files
# They have to be called *.RFI

my @rfi_rpfits_files;
open(LS,"-|") || exec "ls *.RFI";
while(<LS>){
    chomp;
    push @rfi_rpfits_files,$_;
}
close(LS);

# Step 2
# Loop over these RPFITs files
for (my $i=0;$i<=$#rfi_rpfits_files;$i++){
    
    # Step 3
    # Loop over IF number
    for (my $curr_if=1;$curr_if<=2;$curr_if++){
	# we will only use the 2 GHz wide IFs, so IFs 1 & 2
	
	# Step 4
	# Remove tmp.uv if it exists
	system "rm -rf tmp.uv";
	
	# Step 5
	# ATLOD this file and IF
	my $miriad_command="atlod";
	$miriad_command .= " in=".$rfi_rpfits_files[$i];
	$miriad_command .= " ifsel=$curr_if";
	$miriad_command .= " out=tmp.uv";
	$miriad_command .= " options=unflag";
	&run_miriad($miriad_command);

	# Step 6
	# unflag all the data, because currently it will all
	# be flagged
	$miriad_command="uvflag";
	$miriad_command .= " vis=tmp.uv";
	$miriad_command .= " flagval=unflag";
	&run_miriad($miriad_command);

	# Step 7
	# reflag all the bad CABB channels
	$miriad_command="uvflag";
	$miriad_command .= " vis=tmp.uv";
	$miriad_command .= " flagval=flag";
	$miriad_command .= " \"select=amp(0,0.00001)\"";
	&run_miriad($miriad_command);

	# Step 8
	# Make a marker file
	system "touch marker";

	# Step 9
	# split the dataset
	$miriad_command="uvsplit";
	$miriad_command .= " vis=tmp.uv";
	$miriad_command .= " options=clobber";
	&run_miriad($miriad_command);

	# Step 10
	# Figure out which files were just created
	open(LS,"-|") || exec "ls -rt";
	my @created_files;
	my $add_files=0;
	while(<LS>){
	    chomp;
	    my $line=$_;
	    if ($line eq "marker"){
		$add_files=1;
	    } elsif ($add_files==1){
		push @created_files,$line;
	    }
	}
	close(LS);

	# Step 11
	# Loop over all the created files
	for (my $j=0;$j<=$#created_files;$j++){

	    # Step 12
	    # Loop over channel and frequency plots
	    for (my $k=0;$k<2;$k++){
		my $xaxis;
		if ($k==0){
		    $xaxis="chan";
		} elsif ($k==1){
		    $xaxis="freq";
		}

		# Step 13
		# Loop over the valid antenna pairs
		for (my $lowant=1;$lowant<6;$lowant++){
		    for (my $highant=$lowant+1;
			 $highant<=6;$highant++){
			# We assume that there should be .RFI files,
			# and that file 1 is set 1, file 2 is set 2
			# and file 3 is set 3. That way we can determine
			# which baselines are valid from each file
			if ($i==0){
			    # first set, valid pairs are:
			    # 12,14,16,23,25,34,36,45,56
			    if ((($lowant==1)&&
				 (($highant!=2)&&($highant!=4)&&($highant!=6)))
				||(($lowant==2)&&
				   (($highant!=3)&&($highant!=5)))
				||(($lowant==3)&&
				   (($highant!=4)&&($highant!=6)))
				||(($lowant==4)&&
				   ($highant!=5))
				||(($lowant==5)&&
				   ($highant!=6))){
				# this is an invalid combination, move to
				# the next one
				next;
			    }
			} elsif ($i==1){
			    # second set, valid pairs are:
			    # 13,15,24,26
			    if ((($lowant==1)&&
				 (($highant!=3)&&($highant!=5)))
				||(($lowant==2)&&
				   (($highant!=4)&&($highant!=6)))
				||(($lowant!=1)&&($lowant!=2))){
				# this is an invalid combination, move to
				# the next one
				next;
			    }
			} elsif ($i==2){
			    # third set, valid pairs are
			    # 35,46
			    if ((($lowant==3)&&
				 ($highant!=5))
				||(($lowant==4)&&
				   ($highant!=6))
				||(($lowant!=3)&&($lowant!=4))){
				# this is an invalid combination, move to
				# the next one
				next;
			    }
			} elsif ($i>2){
			    # we shouldn't have this many sets
			    die ("can't use more than three sets!\n");
			}
			# this is a valid combination
			
			# Step 14
			# Work out what the logfile should be called
			my $logfilename="$lowant$highant\_";
			$logfilename .= $created_files[$j];
			# replace the frequency with the IF number
			$logfilename=~s/(.*)\.(.*)/$1.IF$curr_if/;
			# and add the xaxis name
			$logfilename .= ".".$xaxis;
			# and the .log extension
			$logfilename .= ".log";
			
			# Step 15
			# Make a spectral plot
			$miriad_command="uvspec";
			$miriad_command .= " vis=".$created_files[$j];
			$miriad_command .= " stokes=xx,yy";
			$miriad_command .= " interval=10";
			$miriad_command .= " device=/null";
			$miriad_command .= " axis=".$xaxis.",amp";
			$miriad_command .= " \"select=ant($lowant)($highant),-auto\"";
			$miriad_command .= " log=".$logfilename;
			&run_miriad($miriad_command);
		    }
		}
	    }
	    # Step 16
	    # Remove the created file
	    my $remove_command="rm -rf";
	    $remove_command .= " ".$created_files[$j];
	    system $remove_command;
	}
    }
}

sub run_miriad {
    my $miriad_command=$_[0];

    print "<$miriad_command>\n";
    system $miriad_command;
}
